import httpClient from "./http-client";

export class MessageCenterService {
  baseUrl = "/api/v1.0/portal/message-center";
  wsBaseUrl = `ws://localhost:8080/portal/ws/message-count`;

  async page(no = 1, size = 10, searchObj) {
    // 前端从1开始计数，后端从0开始计数
    no = no - 1;
    return httpClient.post(`${this.baseUrl}/page?no=${no}&size=${size}`, {
      ...searchObj,
    }).then((res) => {
      if (res.data.data) {
        const page = res.data.data;
        // 前端从1开始计数，后端从0开始计数
        page.number = page.number + 1;
        return Promise.resolve(page);
      } else {
        throw "no data";
      }
    });
  }

  async retrieve(msgId) {
    return httpClient.get(`${this.baseUrl}/retrieve/${msgId}`).then((res) => {
      return Promise.resolve(res.data.data);
    });
  }

  async read(param) {
    const body = {
      ...param,
    };
    return httpClient.post(`${this.baseUrl}/read`, body);
  }
}

const messageCenterService = new MessageCenterService();

export default messageCenterService;